
define(function(){

    var sum=function(){

        $.getJSON('../data/home.json',function(responseText,responseState,responseObj){

              jieXi(responseText[0].data.act_info);
        })

        // 解析数据
        function jieXi(data){
              var datas=data;
              // 便利数组
              for( var objArr of datas){
                var type=objArr.type;

                // 获取轮播图数据
                if(type=='focus'){
                  var $ban=$('#turn>#ban');
                  var newArr=objArr.act_rows;
                  var str=''
                    for(var myData of newArr){
                      var act = myData.activity;
                      var img=act.img;

                      str+=`<img src=`+img+`>`;
                    }

                    $ban.html(str);
                }

                // 每日必抢部分
                if(type=='icon'){

                    var $myDiv=$('#scareBuy');
                  var newArr=objArr.act_rows;
                  var str='';
                  for(var myData of newArr){
                      var act = myData.activity;
                      var img=act.img;
                      var name=act.name;
                      str+=`<div class="sb">
                            <img src=`+img+`>
                            <p>`+name+`</p>
                        </div>`
                  }
                  $myDiv.html(str);

                }
                // 酸奶女王
                if(type=='brand'){
                    var $myDiv=$('#three');
                    var newArr=objArr.act_rows;
                    var str=''

                      for(var j=0; j<newArr.length-1;j++){

                        var act = newArr[j].activity;
                        var img=act.img;

                        str+=`<div>
                          <img src=`+img+`>
                        </div>`
                      }
                      $myDiv.html(str);
                }
                // 便利店
                if(type=='business'){

                  var newArr=objArr.act_rows;

                  for(var index in newArr){

                    if(index==0){
                      var $myDiv=$('#store>.s_logo');
                      var act = newArr[index].act_rows[0].chead_detail;
                      var img=act.img
                      var str=`<img src=`+img+`>`;
                      $myDiv.html(str);
                    }
                    if(index==1){
                      var $myDiv=$('#store>.s_box');
                      var act = newArr[index].act_rows;
                      var str=''
                      for(var cac of act){
                          var cac=cac.cactivity_detail;
                          var img=cac.img;
                          str+=`<div class="">
                              <img src=`+img+`>
                            </div>`

                      }
                      $myDiv.html(str);
                    }
                    if(index==2){
                      var $myDiv=$('#store>.milk');
                      var act = newArr[index].act_rows;
                      var str='';
                      for(var cac of act){
                        var cic=cac.cicons_detail;
                        var img=cic.img;
                        var name=cic.name;
                        str+=`<div class="">
                            <img src=`+img+`>
                            <p>`+name+`</p>
                        </div>`
                      }
                      $myDiv.html(str);
                    }
                    if(index==3){
                      var $myDiv=$('#store>.s_bottom');
                      var act = newArr[index].act_rows;
                      var str='';
                      for(var cac of act){
                        var act=cac.activity;
                        var img=act.img;
                        str+=`<div class="">
                          <img src=`+img+`>
                        </div>`

                      }
                      $myDiv.html(str);

                    }
                  }
                }
                // 下半部分优选水果
                if(type=='category'){
                  // console.log(objArr);
                  var newArr=objArr.act_rows;
                  var $myDiv=$('#con_bottom');
                  var str='';
                  var num=0;
                  for(var myData of newArr){
                    num++;
                    var name=myData.category_detail.name;
                    var img=myData.activity.img;
                    // console.log(img);
                    var goods=myData.category_detail.goods;
                    var color=myData.category_detail.category_color;
                    var str1='';
                    for(var foodObj of goods){
                          var img1=foodObj.app_mimg;
                          var brand_name=foodObj.name;
                          var specifics=foodObj.specifics;
                          var market_price=foodObj.d_price;
                          var d_price=foodObj.price;
                          var org_source_value=foodObj.org_source_value;
                          var pm_desc=foodObj.pm_desc;
                            // console.log(d_price);
                          if(market_price){
                                  if(org_source_value==1){
                                                if(pm_desc){
                                                    str1+=`<div class="wareBox">
                                                            <span class="add">＋</span>
                                                            <img src=`+img1+`>
                                                            <div class="wareBoxTop">
                                                              <h3>`+brand_name+`</h3>
                                                              <span class="jingxuan">精选</span>
                                                              <span class="present">`+pm_desc+`<span>
                                                            </div>
                                                            <div class="wareBoxB">
                                                                <h3>`+specifics+`</h3>
                                                                <p>￥`+d_price+`<span>￥`+market_price+`</span></p>
                                                            </div>
                                                        </div>`
                                                    }else {
                                                      str1+=`<div class="wareBox">
                                                              <span class="add">＋</span>
                                                              <img src=`+img1+`>
                                                              <div class="wareBoxTop">
                                                                <h3>`+brand_name+`</h3>
                                                                <span class="jingxuan">精选</span>
                                                              </div>
                                                              <div class="wareBoxB">
                                                                  <h3>`+specifics+`</h3>
                                                                  <p>￥`+d_price+`<span>￥`+market_price+`</span></p>
                                                              </div>
                                                          </div>`
                                                    }
                                        }else if (org_source_value==2) {
                                                if(pm_desc){
                                                          str1+=`<div class="wareBox">
                                                                  <span class="add">＋</span>
                                                                  <img src=`+img1+`>
                                                                  <div class="wareBoxTop">
                                                                    <h3>`+brand_name+`</h3>
                                                                      <span class="jingxuan" style="border:none"></span>
                                                                      <span class="present">`+pm_desc+`<span>
                                                                  </div>
                                                                  <div class="wareBoxB">
                                                                      <h3>`+specifics+`</h3>
                                                                      <p>￥`+d_price+`<span>￥`+market_price+`</span></p>
                                                                  </div>
                                                              </div>`
                                                          }else {
                                                            str1+=`<div class="wareBox">
                                                                    <span class="add">＋</span>
                                                                    <img src=`+img1+`>
                                                                    <div class="wareBoxTop">
                                                                      <h3>`+brand_name+`</h3>
                                                                        <span class="jingxuan" style="border:none"></span>
                                                                    </div>
                                                                    <div class="wareBoxB">
                                                                        <h3>`+specifics+`</h3>
                                                                        <p>￥`+d_price+`<span>￥`+market_price+`</span></p>
                                                                    </div>
                                                                </div>`
                                                          }
                                        }
                            }else {
                                  if(org_source_value==1){
                                                    if(pm_desc){
                                                          str1+=`<div class="wareBox">
                                                                  <span class="add">＋</span>
                                                                  <img src=`+img1+`>
                                                                  <div class="wareBoxTop">
                                                                    <h3>`+brand_name+`</h3>
                                                                    <span class="jingxuan">精选</span>
                                                                    <span class="present">`+pm_desc+`<span>
                                                                  </div>
                                                                  <div class="wareBoxB">
                                                                      <h3>`+specifics+`</h3>
                                                                      <p>￥`+d_price+`</p>
                                                                  </div>
                                                              </div>`
                                                            }else {
                                                                str1+=`<div class="wareBox">
                                                                        <span class="add">＋</span>
                                                                        <img src=`+img1+`>
                                                                        <div class="wareBoxTop">
                                                                          <h3>`+brand_name+`</h3>
                                                                          <span class="jingxuan">精选</span>
                                                                        </div>
                                                                        <div class="wareBoxB">
                                                                            <h3>`+specifics+`</h3>
                                                                            <p>￥`+d_price+`</p>
                                                                        </div>
                                                                    </div>`
                                                            }
                                              }else if (org_source_value==2) {
                                                    if(pm_desc){
                                                        str1+=`<div class="wareBox">
                                                                <span class="add">＋</span>
                                                                <img src=`+img1+`>
                                                                <div class="wareBoxTop">
                                                                  <h3>`+brand_name+`</h3>
                                                                  <span class="jingxuan" style="border:none"></span>
                                                                  <span class="present">`+pm_desc+`<span>
                                                                </div>
                                                                <div class="wareBoxB">
                                                                    <h3>`+specifics+`</h3>
                                                                    <p>￥`+d_price+`</p>
                                                                </div>
                                                            </div>`
                                                          }else {
                                                            str1+=`<div class="wareBox">
                                                                    <span class="add">＋</span>
                                                                    <img src=`+img1+`>
                                                                    <div class="wareBoxTop">
                                                                      <h3>`+brand_name+`</h3>
                                                                      <span class="jingxuan" style="border:none"></span>
                                                                    </div>
                                                                    <div class="wareBoxB">
                                                                        <h3>`+specifics+`</h3>
                                                                        <p>￥`+d_price+`</p>
                                                                    </div>
                                                                </div>`
                                                          }
                                              }
                            }
                    }
                    str+=`<div id="c_b_box">
                              <div class="c_nav">
                                  <h3 style="color:#`+color+`">`+name+`</h3>
                                  <a href="#">更多<span></span>
                                  </a>
                              </div>
                              <div class="c_img">
                                <img src=`+img+`>
                              </div>
                              <div class="c_wares">`+str1+`</div>
                    </div>
                    <div id="line"></div>
                    `

                    $myDiv.html(str);

                  }


                }




              }
        }












      // 轮播图部分
      banner()
      function banner(){
            //		窗口
          var $ban=$('#ban');
          var $trun = $('#turn');
      //		所有的轮播图
          var $imgs = $('#ban>img');
      //		设置卷轴的宽度
          var w = parseInt($trun.css('width'));
          $ban.width(w*$imgs.length);
      //		定义一个跟随变量,记录当前所展示的图片是第几张图
          var index = 0;
      //		控制图片向后轮播的函数
          function nextFn(){
      //			把当前展示的图片标号增一
            index++;
      //			图片是否达到最后一张,超出后需要清零
            if(index > $imgs.length-1){
              index=0;
            }
      //		让卷轴左偏移量移动
      //		让卷轴左偏移量
          var left1 = index*w *- 1;
          $ban.css('left',left1)
            }
      //		清理定时器:在需要打断自动轮播顺序是,必须清理自动轮播的定时器
          function clearTimer(){
            clearInterval(timer);
      //			重新开启定时器,2秒再次开启轮播
            timer = setInterval(nextFn , 2000);
          }
          var timer = setInterval(nextFn , 2000);
          var ban=document.getElementById('ban');
          touch.on(ban,'swiperight',function(ev){
            nextFn();
            clearTimer();
          });
          touch.on(ban,'swipeleft',function(ev){
            index--;
            if(index<0){
              index = index.length-1;
            }
            var left = index*w*-1;
            $ban.css('left',left);
            clearTimer();
          })
        }

    };

    return{
      sum:sum
    }
})
